home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d21 / dvglue.arc / TVJNEW.C < prev    next >
C/C++ Source or Header  |  1990-01-09  |  5KB  |  168 lines

  1. /*=======================================================*/
  2. /*  TVJNEW.C                                             */
  3. /*                                                       */
  4. /*  (c) Copyright 1988 Ralf Brown  All Rights Reserved   */
  5. /*  May be freely copied for noncommercial use, so long  */
  6. /*  as this copyright notice remains intact, and any     */
  7. /*  changes are marked in the comment blocks preceding   */
  8. /*  functions.                                           */
  9. /*=======================================================*/
  10.  
  11. #include <string.h>
  12. #include <stdarg.h>
  13. #include "tvapi.h"
  14.  
  15. #define STACKSIZE 256
  16.  
  17. OBJECT _TV_newapp_mutex_ = NULL ;
  18.  
  19. /*======================================================*/
  20.  
  21. struct EXEC_block {
  22.        int env ;  /* segment of environment */
  23.        char far *cmdline ;
  24.        char far *fcb_1 ;
  25.        char far *fcb_2 ;
  26.     } ;
  27.  
  28. static char *new_program ;
  29. static struct EXEC_block *exec_block ;
  30.  
  31. /*======================================================*/
  32. /* launch_app  fork to here, and then EXEC the program  */
  33. /*   Ralf Brown 5/10/88                                 */
  34. /*   Ralf Brown 6/6/88  added own spawn routine         */
  35. /*======================================================*/
  36.  
  37. static void launch_app(int parent)
  38. {
  39.    struct EXEC_block *exec_blk = exec_block ;
  40.    char *program = new_program ;
  41.    union REGS regs ;
  42.    struct SREGS sregs ;
  43.  
  44.    (void) parent ;  /* satisfy TurboC's warnings about unused parameters */
  45.    /* we've now got copies of the static variables, so we can unblock */
  46.    TVunlock(_TV_newapp_mutex_) ;
  47.    TVwin_swrite(NIL,"Loading...\r\n") ;
  48.    /* EXEC the program and get its exit code */
  49. #if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
  50.    sregs.ds = _DS ;
  51.    regs.x.dx = (int) program ;
  52.    sregs.es = _DS ;
  53.    regs.x.bx = (int) exec_blk ;
  54. #else
  55.    sregs.ds = FP_SEG( program ) ;
  56.    regs.x.dx = FP_OFF( program ) ;
  57.    sregs.es = FP_SEG( exec_blk ) ;
  58.    regs.x.bx = FP_OFF( exec_blk ) ;
  59. #endif 
  60.    regs.x.ax = 0x4B00 ;  /* EXEC with load&run option */
  61.    intdosx( ®s, ®s, &sregs ) ;
  62. #if 0  /* waiting for completion code hangs process */
  63.    if ((regs.x.cflag & 1) == 0)
  64.       {
  65.       regs.h.ah = 0x4D ;         /* if load was successful, wait for completion */
  66.       intdos( ®s, ®s ) ;
  67.       }
  68. #endif 0
  69.    free(program) ;
  70.    free((char *) exec_blk->fcb_1) ;
  71.    free((char *) exec_blk->cmdline) ;
  72.    free(exec_blk) ;
  73. }
  74.  
  75. /*======================================================*/
  76. /* TVapp_new  create new application in current process */
  77. /*   Ralf Brown 5/10/88                                 */
  78. /*======================================================*/
  79.  
  80. OBJECT cdecl TVapp_new(OBJECT win,int row,int col,int rows,int cols,char *program, ...)
  81. {
  82.    OBJECT new_app = NIL ;
  83.    va_list args ;
  84.    char *arg ;
  85.    int cmd_len = 0 ;
  86.    char *cmd_line, *cmd, *rest ;
  87.    struct fcb *fcb1, *fcb2 ;
  88.  
  89.    /* allocate space for the commandline and FCBs */
  90.    if ((cmd_line = malloc(128)) != NULL)
  91.       cmd = cmd_line+1 ;
  92.    else
  93.       goto error2 ;
  94.    if ((fcb1 = malloc(2*sizeof(struct fcb))) != NULL)
  95.       fcb2 = fcb1+1 ;
  96.    else
  97.       {
  98.       free(cmd_line) ;
  99.       goto error2 ;
  100.       }
  101.    /* we'll be overwriting static vars, so can only have one copy here at a time */
  102.    if (!TVisobj(_TV_newapp_mutex_))
  103.       _TV_newapp_mutex_ = TVmbx_new() ;
  104.    TVlock(_TV_newapp_mutex_) ;
  105.    if ((new_program = (char *)malloc(strlen(program)+1)) != NULL)
  106.       strcpy(new_program, program) ;
  107.    else
  108.       {
  109.       free(cmd_line) ;
  110.       free(fcb1) ;
  111.       goto error ;
  112.       }
  113.    if ((exec_block = malloc(sizeof(struct EXEC_block))) != NULL)
  114.       {
  115.       exec_block->env = 0 ;
  116.       exec_block->cmdline = (char far *)cmd_line ;
  117.       exec_block->fcb_1 = (char far *)fcb1 ;
  118.       exec_block->fcb_2 = (char far *)fcb2 ;
  119.       }
  120.    else
  121.       {
  122.       free(cmd_line) ;
  123.       free(fcb1) ;
  124.       free(new_program) ;
  125.       goto error ;
  126.       }
  127.    /* then concatenate the args to form the commandline */
  128.    va_start(args, program) ;
  129.    (void) va_arg( args, char * ) ;  /* skip argv[0] */
  130.    while ((arg = va_arg(args, char *)) != NULL)
  131.       {
  132.       while (*arg && cmd_len < 126)
  133.          {
  134.          *cmd++ = *arg++ ;
  135.          cmd_len++ ;
  136.          }
  137.       if (cmd_len >= 126)
  138.          {
  139.          cmd_len = 126 ;
  140.          break ;
  141.          }
  142.       else
  143.          {
  144.          *cmd++ = ' ' ;  /* separate args by blanks */
  145.          cmd_len++ ;
  146.          }
  147.       }
  148.    cmd_line[0] = cmd_len ;
  149.    cmd_line[cmd_len+1] = '\r' ;
  150.    va_end( args ) ;
  151.    /* now set up the FCBs */
  152.    rest = parsfnm( cmd_line+1, (struct fcb *)fcb1, 1 ) ;  /* 1=ignore leading separators */
  153.    if (rest)  /* was first parse successful? */
  154.       rest = parsfnm( rest, (struct fcb *)fcb2, 1 ) ;
  155.    /* and launch the program */
  156.    new_app = TVnew_task(win,NULL,row,col,rows,cols,NULL,STACKSIZE,launch_app) ;
  157.    if (new_app)
  158.       TVlock(_TV_newapp_mutex_) ;  /* wait until launch_app() releases exclusion */
  159.    else
  160.       return NIL ;
  161. error:
  162.    TVunlock(_TV_newapp_mutex_) ;
  163. error2:
  164.    return new_app ;
  165. }
  166.  
  167. /* End of TVJNEW.C */
  168.